﻿using nplJobsCommon.Common;
using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nplJobsDAL.Select
{
    public class SelectJobSeekerByCredentials : DataAccessBase
    {
        public string UserID { get; set; }

        public SelectJobSeekerByCredentials()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectJobSeekerbyCredentials.ToString();
        }

        #region GetJobSeeker
        public UserCredential GetJobSeekerUserCredentials()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobSeeker;

            try
            {
                SelectJobSeekerParameters selectJobSeekerParameters = new SelectJobSeekerParameters(this.UserID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectJobSeekerParameters.Parameters;
                dsJobSeeker = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetJobSeekerUserCredential(dsJobSeeker);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion



        #region SQL parameters for selecting job seeker by id
        public class SelectJobSeekerParameters
        { 
            private SqlParameter[] _parameters;

            public SelectJobSeekerParameters(string email)
            {
                this.Email = email;
                Build();
            }

            #region spAddContact parameters
            /*
         
         */
            #endregion

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@Email", Email),
                };

                Parameters = parameters;
            }

            public string Email { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion

        #region GetJobSeekerUserCredential
        private UserCredential GetJobSeekerUserCredential(DataSet dsJobSeeker)
        {
            try
            {
                UserCredential userCredential = (from n in dsJobSeeker.Tables[0].AsEnumerable()
                             select new UserCredential
                             {
                                 PasswordFormat = n.Field<string>("PasswordFormat"),
                                 Password = n.Field<string>("Password"),
                                 PasswordSalt = n.Field<string>("PasswordSalt"),

                             }).FirstOrDefault();

                return userCredential;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}

